def yield_end(c):
    end = 0
    for i in range(c):
        end = end * 10 + 9
    return end


def yield_start(c):
    start = 1
    for i in range(c - 1):
        start = start * 10
    return start


if __name__ == "__main__":
    cases = int(input())
    result=[]
    for i in range(cases):
        ls = list(map(lambda x: int(x), input().strip().split()))
        c, w = ls[0], ls[1]
        count = 0
        start = yield_start(c)
        end = yield_end(c)

        for each in range(start, end + 1):
            if each % 10 == w and each % 3 == 0:
                # print(each)
                count += 1
        result.append(count)
    # print(result)
    index=0
    for each in result:
        print("case #%d:\n%d" % (index,each))
        index+=1
